package com.zy.rpc.server.config;

import com.zy.rpc.common.register.RegistryService;
import com.zy.rpc.common.register.impl.RegistryServiceImpl;
import com.zy.rpc.server.RpcServerStart;
import com.zy.rpc.server.transport.RpcServer;
import com.zy.rpc.server.transport.impl.NettyRpcServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @author zy
 * @date 2023/4/23 17:24
 * 统一做rpc启动的bean管理
 */



@Configuration
@EnableConfigurationProperties(RpcServerProperties.class)
public class RpcServerAutoConfiguration {

    @Autowired
    private RpcServerProperties rpcServerProperties;
    @Bean
    @ConditionalOnMissingBean(RegistryService.class)
    public RegistryService RegistryService() throws Exception {
       return new RegistryServiceImpl(rpcServerProperties.getRegistryAddr());
    }

    @Bean
    @ConditionalOnMissingBean(RpcServer.class)
    public RpcServer rpcServer(){
       return  new NettyRpcServer();
    }

    @Bean
    @ConditionalOnMissingBean(RpcServerStart.class)
    public RpcServerStart rpcServerStart(@Autowired RegistryService registryService,
                                               @Autowired RpcServer rpcServer,
                                               @Autowired RpcServerProperties rpcServerProperties){
        return new RpcServerStart(registryService, rpcServer, rpcServerProperties);
    }
}
